/**
 * Foxit Software Inc.
 * Copyright(C) 2005-2014, all rights reserved.
 * The following code is copyrighted and contains proprietary information
 * and trade secrets of Foxit Software Inc.

 * Description: Comments Annot Handler
 * Author: shaojie zhuo
 */

define(function (require, exports, module) {
    /**
     * @param readerApp
     * @param annotType
     * @param name
     * @param pCommentAnnotPlugin
     * @constructor
     * @extends WebPDF.IBA_AnnotHandler
     */
    WebPDF.CommentAnnotHandler = function (readerApp, annotHandlerManager, annotType) {
    	this.readerApp = readerApp;
    	this.annotType = annotType;
        this.annotHandlerManager = annotHandlerManager;
    };

    WebPDF.CommentAnnotHandler.isEditableCommentsType = function (sType) {
        if (sType == WebPDF.PDFData.CommentAnnotType.COMMENTTYPE_UNDERLINE ||
            sType == WebPDF.PDFData.CommentAnnotType.COMMENTTYPE_HIGHLIGHT)
            return true;
        else
            return false;
    };

    WebPDF.CommentAnnotHandler.prototype =
    {
    	getType: function () { return this.annotType; },

    	canAnswer: function (annot, bAllowSelectTextTool)
    	{
            /*if(!this.readerApp.isEditable()) {
                return false;
            }*/

    		var type = annot.getSubType();
    		if (!WebPDF.CommentAnnotHandler.isEditableCommentsType(type)) {
                return false;
            }
    		var tool = this.readerApp.getCurToolHandler();
    		if (tool){
    			var toolName = tool.getName();
    			if (toolName===WebPDF.Tools.BA_SELANNOT_TOOLNAME || toolName === WebPDF.Tools.TOOL_NAME_HAND ||
                    toolName === this.annotType){
    				return true;
    			}
                if (bAllowSelectTextTool != undefined && bAllowSelectTextTool) {
                    if (toolName === WebPDF.TOOL_NAME_SELECTTEXT) {
                        return true;
                    }
                }
    		}
    		return false;
    	},
        canMove: function (subType) {
            return false;
        },
        moveAnnot: function(pageView, selectedAnnot,
                          offsetX, offsetY ,newPosX, newPosY){
        },
        deleteAnnot: function(pageView, annot){
            if(!this.readerApp.isEditable()){
                return;
            }
            var annotUIManager = this.annotHandlerManager.getUIManager();
            var annotName = annot.getAnnotName();
            var annotDivID = annotUIManager.getAnnotDivID(pageView, annotName);
            $("#" + annotDivID).remove();
            var annotShowDivID = annotUIManager.getCommentAnnotDivID(pageView, annotName);
            $("#" + annotShowDivID).remove();
            var popup = annot.getPopup();
            if (popup == null || annot.getHeadAnnot() != null)  {
                return ;
            }
            var popupDivID = annotUIManager.getAnnotPopupDivID(pageView, annotName);
            $("#" + popupDivID).remove();
        },
        onDraw: function(pageView, selectedAnnot, isRedraw, extraScale){
        },
    	onSelected: function(pageView, annot){
            if(!this.readerApp.isEditable()){
                return;
            }
            var annotDivID = this.annotHandlerManager.getUIManager().getAnnotDivID(pageView, annot.getAnnotName());
            var $annotDiv = $("#" + annotDivID);
            $annotDiv.addClass("fwr-annot-selected");
            //.fwrContextMenu(this.annotHandlerManager.getContextMenuId(), {
//                onSelect: function (e, context) {
//                    try {
//                    	var menuName = $(this).attr("menuname");
//                    	if (menuName == "DeleteAnnot") {
//                    		var annotHandlerMgr = WebPDF.Tool.getReaderApp().getPluginByName(WebPDF.BASEANNOT_PLUGIN_NAME).getAnnotHandlerMgr();
//                    		annotHandlerMgr.deleteSelection();
//                    	}
//                    }
//                    catch (ex) {
//                        console.error(ex);
//                    }
//                }
//            },document.getElementById(_readerApp.getMainView().getMainFrameID()));
            if (WebPDF.Environment.mobile) {
                Hammer($annotDiv.get(0)).on("hold", function (event) {
                    event.stopPropagation();
                    $(this).trigger("contextmenu.fwrContextMenu", event.gesture.touches[0]);
                });
            }
            return true;
        },
        onDeSelected: function(pageView, annot){
            if(!this.readerApp.isEditable()){
                return;
            }
            var annotDivID = this.annotHandlerManager.getUIManager().getAnnotDivID(pageView, annot.getAnnotName());
            $("#" + annotDivID).removeClass("fwr-annot-selected").nofwrContextMenu();
        },
        onFocus: function(pageView, annot){
            this.annotHandlerManager.getMarkupAnnotHandler().onFocus(pageView, annot);
        },
    	onLButtonDown: function(pageView,annot, event){
    		return this.onSelected(pageView,annot);
    	},
        onLButtonUp: function(pageView,annot, event){

        },
    	onLButtonDblClk: function(pageView, annot, event){
            this.annotHandlerManager.getMarkupAnnotHandler().onLButtonDblClk(pageView, annot,event);
    	},
        onMouseOver: function(pageView, annot, event){
            this.annotHandlerManager.getMarkupAnnotHandler().onMouseOver(pageView, annot,event);
        },
        onMouseLeave : function (pageView, annot, event) {
            this.annotHandlerManager.getMarkupAnnotHandler().onMouseLeave(pageView, annot,event);
        },
        onUpdatePosition: function(pageView, annot, visibleRect){
            var pdfPage = pageView.getPDFPage();
            var annotPDFRect = annot.getRect();
            var annotDocRect = pageView.pdfToDoc(annotPDFRect, true);
            var annotUIManager = this.annotHandlerManager.getUIManager();
            var annotDivID = annotUIManager.getAnnotDivID(pageView, annot.getAnnotName());
            $("#" + annotDivID).css({
                    left: annotDocRect.left,
                    top: annotDocRect.top,
                    width: WebPDF.RectUtils.width(annotDocRect),
                    height: WebPDF.RectUtils.height(annotDocRect)
            });
            var subType = annot.getSubType();
            var quadPoints = annot.getQuadPoints();
            var bUseQuad = false;
            if (quadPoints != null) {
                var quadRectArray = this.annotHandlerManager.quadPointsToRectArray(quadPoints, subType, pdfPage.getPageMatrix());
                if (quadRectArray != null) {
                    for (var j = 0; j < quadRectArray.length; j++) {
                        var rect = quadRectArray[j];
                        rect = pageView.pdfToDoc(rect, true);
                        var quadRectDivID = annotUIManager.getCommentAnnotQuadDivID(pageView, annot.getAnnotName(), j);
                        $("#" + quadRectDivID).css({
                            left: rect.left,
                            top: rect.top,
                            width: WebPDF.RectUtils.width(rect),
                            height: WebPDF.RectUtils.height(rect)
                        });
                    }
                    bUseQuad = true;
                }
            }
            if (!bUseQuad) {
                var rectDivId = annotUIManager.getCommentAnnotRectDivID(pageView, annot.getAnnotName());
                $("#" + rectDivId).css({
                    left: annotDocRect.left,
                    top: annotDocRect.top,
                    width: WebPDF.RectUtils.width(annotDocRect),
                    height: WebPDF.RectUtils.height(annotDocRect)
                });
            }
        }
    };



    return WebPDF.CommentAnnotHandler;
});